/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package main

import (
	"flag"
	"log"
	"os"
	"time"

	"gitee.com/WisdomClassroom/cli/action"
	"gitee.com/WisdomClassroom/cli/glb"
	"gitee.com/WisdomClassroom/core"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func main() {
	core.PublicFlag(BuildTag, BuildGo, BuildTime)
	flag.Parse()

	if core.FlagHelp {
		flag.Usage()
		return
	}
	if core.FlagVersion {
		core.PublicVersion()
		return
	}
	if !core.FlagUseSqlite && !core.FlagUsePG {
		flag.Usage()
		return
	}

	glb.Logger = core.NewLogger(core.FlagLogLevel)

	config := &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: logger.New(
			log.New(os.Stdout, "", log.LstdFlags),
			logger.Config{
				LogLevel: core.GetGormLogLevel(),
				Colorful: true,
			},
		),
	}
	if core.FlagUseSqlite {
		action.InitDBSqlite(config)
	} else if core.FlagUsePG {
		action.InitDBPGSQL(config)
	} else {
		flag.Usage()
	}

	time.Sleep(1 * time.Second / 5)
}
